#include "PermissionsTable.h"
#include "Ext/FilterQuery.h"


namespace TCServer
{
    namespace Store
    {
        void PermissionsTable::create()
        {
            throw std::logic_error("" __FILE__ ", TCServer::Store::PermissionTable::create() is not implemented.");
        }

        void PermissionsTable::insert(quint64 userid, Type::Permission permission)
        {
            FilterQuery q;
            q.prepare("INSERT INTO permissions (userId, permission)"
                "VALUES(?, ?);");
            q.addBindValue(userid);
            q.addBindValue((uint)permission);
            return q.exec();
        }

        void PermissionsTable::remove(quint64 userid, Type::Permission permission)
        {
            FilterQuery q;
            q.prepare(R"(
            DELETE FROM `tc`.`permissions`
            WHERE `userId`=? and `permission`=?;
)");
            q.addBindValue(userid);
            q.addBindValue((uint)permission);
            return q.exec();
        }

        QSet<Type::Permission> PermissionsTable::query(quint64 userid)
        {
            FilterQuery q;
            q.prepare("SELECT permission from permissions where userId = ?;");
            q.addBindValue(userid);
            QSet<Type::Permission> permissions;
            q.exec();
            {
                while (q.next())
                    permissions.insert((Type::Permission)q.value(0).toUInt());
            }
            return permissions;
        }

    }
}